package com.huifaapp.mall.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.PublicCountAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.PubliCountBean;
import com.huifaapp.mall.dialogs.LoadingDialog;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by duanyu on 2018/2/2.
 * 邀请团队队页
 */

public class PublicCountActivity extends AppCompatActivity {

    private XRecyclerView activity_publiccount_xRecyclerView;
    private  String inviteRecordURL= ApiManager.BASE_NIUPAI_URL+"users/inviteRecord";
    private  int curr;
    private List<PubliCountBean.DataBean.ListBean> listBeanlist;
    private PublicCountAdapter publicCountAdapter;
    private ImageView activity_publiccount_destroy;
    LoadingDialog dialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publiccount);
        dialog = new LoadingDialog(PublicCountActivity.this);
        dialog.setContent("正在加载数据");
        dialog.show();
        initView();
        initHttp();
    }

    private void initHttp() {
        Map<String,String>map=new HashMap<>();
        map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
        map.put("appId", AUniqueIdentifier.getDeviceId(PublicCountActivity.this));
        map.put("start",curr+"");
        map.put("length",10+"");
        Log.e("邀请团队接口",inviteRecordURL+"?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(PublicCountActivity.this));

        OkHttpUtils.post().params(map).url(inviteRecordURL).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                activity_publiccount_xRecyclerView.refreshComplete();
                Log.e("邀请团队接口response",response);
                initData(response);


            }
        });
    }

    private void initData(String response) {
        Gson gson = new Gson();
        PubliCountBean publiCountBean = gson.fromJson(response, PubliCountBean.class);
        if (publiCountBean != null && publiCountBean.getData() != null && publiCountBean.getData().getList() != null) {
            if (curr == 0) {
                listBeanlist.clear();
            }
            listBeanlist.addAll(publiCountBean.getData().getList());
            publicCountAdapter = new PublicCountAdapter(listBeanlist,PublicCountActivity.this);
            activity_publiccount_xRecyclerView.setLayoutManager(new LinearLayoutManager(PublicCountActivity.this));
            activity_publiccount_xRecyclerView.setAdapter(publicCountAdapter);
            publicCountAdapter.notifyDataSetChanged();
            dialog.dismiss();
        }
    }
    private void initView() {
        listBeanlist = new ArrayList<>();
        activity_publiccount_xRecyclerView = (XRecyclerView) findViewById(R.id.activity_publiccount_XRecyclerView);

        activity_publiccount_destroy = (ImageView) findViewById(R.id.activity_publiccount_destroy);
        activity_publiccount_destroy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        activity_publiccount_xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                curr = 0;
                initHttp();
            }

            @Override
            public void onLoadMore() {
                curr++;
                initHttp();
            }
        });
        initHttp();
         }

}
